<template>
  <div class="app-container">
      <el-form ref="nodeForm" :rules="rules" :model="nodeForm" label-position="left" label-width="70px" style="margin-left:50px;">

        <el-form-item label="名称" prop="title">
          <el-input v-model="nodeForm.title" />
        </el-form-item>
        <el-form-item label="席位">
          <el-input-number v-model="nodeForm.seat" :min="1" label="席位"></el-input-number>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="nodeForm.description" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="描述" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="nodeForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="saveNode()">
        确认
      </el-button>
      <el-button v-if="nodeForm.status.value === 5" type="danger" @click="handleDelete(nodeForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import {  saveNode, getNodeDetail, removeNodeById } from '@/api/foundation/node'


import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      pickerOptions: {
          disabledDate(time) {
            return time.getTime() < Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      startedOptions:[
        {
          label:"登录时",
          value:1
        },
        {
          label:"启动时",
          value:2
        },
        {
          label:"空闲",
          value:3
        },
        {
          label:"发生事件时",
          value:4
        },
        {
          label:"创建/修改任务时",
          value:5
        },
        {
          label:"当连接到用户会话时",
          value:6
        },
        {
          label:"当从用户会话连接断开时",
          value:7
        }
      ],
      nodeForm: {
        ID: undefined,
        remark: '',
        seat:1,
        title: '',
        status: {
          value:1
        }
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  watch:{
  },
  created() {
    let nodeID = this.$route.query && this.$route.query.nodeID
    if (nodeID !== undefined) {
      nodeID = parseInt(nodeID)
      if (!isNaN(nodeID)) {
        this.listLoading = true
        getNodeDetail(nodeID).then(response => {
          if (response.status === 200) {
            this.nodeForm = response.data
            console.log(this.nodeForm)
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'node' })
    },
    cleanNodeTask:function(missoinForm){
    },
    saveNode() {
      let _this = this
      this.$refs['nodeForm'].validate((valid) => {
        if (valid) {
          try {
            _this.nodeForm.status = _this.nodeForm.status.value;
            if(_this.nodeForm.status == undefined){                
              _this.nodeForm.status = 6
            }
          } catch (error) {
            _this.nodeForm.status = 6;
          }
          if(_this.nodeForm.hasOwnProperty('ID')){
            if(_this.nodeForm.ID !== undefined){
              _this.nodeForm.nodeID = _this.nodeForm.ID;
            }

          }
          saveNode(_this.nodeForm).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'node' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
          })
        }
      })
    },
    handleDelete(row, index) {
      removeNodeById(row.ID).then(() => {
        this.$notify({
          title: 'Success',
          message: 'Update Successfully',
          type: 'success',
          duration: 2000
        })
        this.handleCannel()
      })
    }
  }
}
</script>
